class Adjoin {
    vertices // 顶点集合
    quantity // 顶点数量
    adjList // 临接矩阵
    constructor(vertices) {
        this.vertices = vertices
        this.quantity = vertices.length;
        this._init()
    }

    _init() {
        // this.adjList = Array.from({
        //     length: this.quantity * this.quantity
        // })

        this.adjList =
            Array.from({
                length: this.quantity
            }, x => Array.from({
                length: this.quantity
            }, y => 0));
    }

    //注册临接点
    addVerticesInAdjList(id, sides) {
        const pIndex = this._getIndex(this.vertices, id)
        sides.forEach((item) => {
            const index = this._getIndex(this.vertices, item)
            // this.adjList[pIndex * this.quantity + index] = 1;
            this.adjList[pIndex][index] = 1
            // this.adjList[index][pIndex] = 1
        });
        // console.log(this.adjList, 1111111111)
    }

    _getIndex(arr, id) {
        let _index = -1
        arr.forEach((item, index) => {
            if (item.value_id == id.value_id) {
                _index = index
            }
        })
        return _index


    }

    // 获取定点的边
    getVertexEdge(id) {
        let edgs = []
        const pIndex = this.vertices.indexOf(id);
        const vertexes = this.adjList[pIndex].map((item, index) => {
            if (item) {
                edgs.push(this.vertices[index])
            }
        })
        return edgs
    }
}

class Graph extends Adjoin {
    constructor(commoditySpecs, data) {
        super(commoditySpecs.reduce((total, current) => [
            ...total,
            ...current.list,
        ], []));
        this.commoditySpecs = commoditySpecs;
        this.data = data;
        // 但规格矩阵创建
        this.initCommodity();
    }

    initCommodity() {
        this.data.forEach(items => {
            items.specs.forEach(item => {
                this.addVerticesInAdjList(item, items.specs)
            })
        })
    }

}
export {
    Graph
}